﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Panel_TopLeft : ObjectBase
{
	[HideInInspector] public bool needUpdate;

	const float EXP_SLIDER_COLOR_BOUNDARY = 0.7f;

	public UILabel expLabel;
	public UISlider expSlider;
	public UILabel userNameLabel;
	public UILabel userLevelLabel;
	public UILabel buffNameLabel;
	public UILabel buffRemainTimeLabel;
	public UILabel orderLabel;
	public UILabel kinaLabel;
	public UILabel cashLabel;

	Dictionary<int, int> characterExpByLevelArray;

	void Awake()
	{
		needUpdate = true;

		InitCharacterExpByLevelArray();
	}

	string SetColordName(string name)
	{
        if (SceneController.CrrScene != null)       // 141003 희소 (개별씬 시작시 에러나는듯? 방어 코드삽입)
		    return  ( SceneController.CrrScene.Equals("Main") ? "[666666]" : "[ffffff]" ) + name;
        else
            return "[ffffff]"  + name;
	}

    void Update()
    {
        if (needUpdate)
        {
            UpdateUI();
            needUpdate = false;
        }
    }

	public void UpdateUserName()
	{
        if (UserData.Instance.userModel != null)// 141003 희소 (개별씬 시작시 에러나는듯? 방어 코드삽입)
		    userNameLabel.text = SetColordName(UserData.Instance.userModel.name);
	}

	public void UpdateUI()
	{
		if ( UserData.Instance.userModel == null )
		{
			Debug.LogError("UserData is null");
			return;
		}

		userNameLabel.text = SetColordName(UserData.Instance.userModel.name);
		userLevelLabel.text = UserData.Instance.userModel.level.ToString();
		buffNameLabel.text = "유스티엘의 축복";
		buffRemainTimeLabel.text = "99:99";

		float expValue = (float)UserData.Instance.userModel.exp  / (float)characterExpByLevelArray[UserData.Instance.userModel.level];

		// Exp 슬라이더에는 퍼센트 값이 아닌 소수점 데이터 사용
		expSlider.value = expValue;

		// Exp 값에 따라서 슬라이더 컬러값 적용
		OnChangeValue_Exp();

		// Exp 값의 퍼센트화
		float expPercentValue = expValue * 100.0f;

		// Exp Label에는 퍼센트 값 + 소수점 자리 조정값 사용
		float expRoundPercentValue = Mathf.Floor(expPercentValue * 100.0f) / 100.0f;
		expLabel.text = expRoundPercentValue.ToString() + "%";

		orderLabel.text = string.Format("{0}[999999] / {1}[-]", UserData.Instance.userModel.orderPoint.ToString(), UserData.Instance.userModel.maxOrderPoint.ToString());
		kinaLabel.text = string.Format("{0:n0}", UserData.Instance.userModel.kina);
		cashLabel.text = string.Format("{0:n0}", UserData.Instance.userModel.cash);
	}

	void InitCharacterExpByLevelArray()
	{
		characterExpByLevelArray = new Dictionary<int, int>();
		for (int i = 0; i < GameData.Inst.character.Length; i++)
		{
			DCharacter c = GameData.Inst.character[i];
			characterExpByLevelArray[c.charLevel] = c.charExp;
		}
	}

	public void OnClick_CheatPopup()
	{
		CheatManager.Instance.Show();
	}

	#region Top Left Area
	// 공지
	public void OnClick_Order()
	{
		Debug.Log("OnClick_Order");
        //TODO:TEMP-order point reset
        Popup_TwoButton sc = PopupManager.PushPopup(PopupKind.TwoButton) as Popup_TwoButton;
        sc.SetPopupWithTitleAndBody("Point", Localization.Get("uiPopupMainCaution1"));
        sc.SetPopupWithYesNoString(Localization.Get("uiCommonTextYes"), Localization.Get("uiCommonTextNo"));
        sc.SetEventOK(gameObject, "OnClick_Order_Proc");
	}

    //TODO:TEMP-order point reset
    void OnClick_Order_Proc()      // 확인 팝업 핸들러
    {
        NetworkManager.Instance.C2S_CHEAT_RESOURCE("/cheat/order", 10000, (receiveData) =>
        {
            if (receiveData.ecode != 0)
                return;
            UserData.Instance.userModel.orderPoint = (short)Mathf.Min(UserData.Instance.userModel.orderPoint + 10000, UserData.Instance.userModel.maxOrderPoint);
            needUpdate = true;
        });
    }
	
	// 우편함
	public void OnClick_Cash()
	{
		Debug.Log("OnClick_Cash");
	}
	
	// 옵션
	public void OnClick_Kina()
	{
		Debug.Log("OnClick_Kina");
	}

	public void OnChangeValue_Exp()
	{
		if (expSlider.value < 0.5f)
			expSlider.foregroundWidget.GetComponent<UISprite>().color = GetRGBColor(0.0f, 138.0f, 163.0f);
		else if (expSlider.value < 0.75f)
			expSlider.foregroundWidget.GetComponent<UISprite>().color = GetRGBColor(0.0f, 212.0f, 244.0f);
		else
			expSlider.foregroundWidget.GetComponent<UISprite>().color = GetRGBColor(161.0f, 245.0f, 255.0f);
	}

	Color GetRGBColor(float _r, float _g, float _b, float _a = 255.0f)
	{
		Debug.Log ("GetRGBColor");
		return new Color(_r / 255.0f, _g / 255.0f, _b / 255.0f, _a / 255.0f);
	}
	#endregion
}
